package ro.gateway.aida.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.utils.HttpUtils;
import ro.xblue.translator.LanguageBean;
import ro.xblue.translator.TranslatorUtils;

/**
 * Created by IntelliJ IDEA.
 * User: smike
 * Date: Apr 19, 2004
 * Time: 9:12:45 PM
 * To change this template use File | Settings | File Templates.
 */
public class PropertyList$ViewAction extends Action {
	public ActionForward execute(ActionMapping mapping,
						    ActionForm form,
						    HttpServletRequest request,
						    HttpServletResponse response)
		 throws Exception {

		PersistenceToken token = PersistenceManager.
			 tokenLookup(this.getServlet().getServletContext());
		HttpSession session = request.getSession();
		String event = HttpUtils.getValidTrimedString(request, "event", null);
		PropertyList list = (PropertyList) session.getAttribute(LIST);

		if ("start".equals(event)) {
			String list_name = HttpUtils.getValidTrimedString(request, "name", null);
			list = PropertyListDB.getManager(token).getList(list_name);
			if (list != null) {
				session.setAttribute(LIST, list);
			} else {
				session.removeAttribute(LIST);
				response.sendError(HttpServletResponse.SC_NOT_FOUND);
				return null;
			}
		}

		if (list != null) {
			LanguageBean lang_bean = TranslatorUtils.getCurrentLanguage(request, this.getServlet().getServletContext());
			list.setViewLang(lang_bean.language);
		}


		return mapping.findForward("jsp_page");
	}

	public static final String LIST = "propslist_list";
}
